package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.entity.VisitCount;
import com.zhang.mapper.VisitCountMapper;
import com.zhang.service.VisitCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张立松
 * @since 2023-05-09
 */
@Service
public class VisitCountServiceImpl extends ServiceImpl<VisitCountMapper, VisitCount> implements VisitCountService {

    @Override
    public Boolean incrVisitCount() {
        VisitCount visitCount = this.getOne(new QueryWrapper<VisitCount>().eq("date", LocalDate.now()));
        if(visitCount != null){
            visitCount.setCount(visitCount.getCount()+1);
            return this.updateById(visitCount);
        }

        Integer random = (new Random()).nextInt(12)+2;
        visitCount = new VisitCount();
        visitCount.setCount(1).setDate(LocalDate.now()).setRandom(random);

        return this.save(visitCount);
    }
}
